package shoppingCartFrame;

import java.util.Enumeration;
import java.util.*;

import javax.swing.JPanel;


public class PanelRegistry extends Observable{

	// The single copy of the registry
		protected static PanelRegistry registry;
		
		// The table of frames
	    protected Hashtable panels;

	//Used for generating unique frame names
	    protected int nextUnique;
		
	    protected PanelRegistry()
	    {
	         panels = new Hashtable();
	         nextUnique = 0;
	    }

	//Returns the long instance of the registry. If there isn't a registry
	//yet, it creates one.

	    public synchronized static PanelRegistry instance()
	    {
	         if (registry == null) {
	              registry = new PanelRegistry();
	         }
	         return registry;
	    }
	    
	 // Adds a new frame to the registry - stores it in the table and
	 // sends a notification to its observers.

	      public synchronized void addPanel(String name, JPanel newPanel)
	      {
	           panels.put(name, newPanel);
	           setChanged();
	           notifyObservers(new PanelRegistryEvent(
	                PanelRegistryEvent.ADD_PANEL,
	                name, newPanel));
	      }
	      
	   // Adds a new frame to the registry - stores it in the table and
	   // sends a notification to its observers. If uniqueName is false, the
	   // frame's name is non-unique. Store the frame in a table with a
	   // unique version of the name (appends <#> to the name where # is
	   // a constantly increasing number).

	        public synchronized void addPanel(String name, JPanel newPanel,
	             boolean uniqueName)
	        {
	             if (!uniqueName && (panels.get(name) != null)) {
	                  name = name + "<"+nextUnique+">";
	                  nextUnique++;
	             }

	             panels.put(name, newPanel);
	             setChanged();
	             notifyObservers(new PanelRegistryEvent(
	                  PanelRegistryEvent.ADD_PANEL,
	                  name, newPanel));
	        }
	        
	     // removes a frame from the table and notifies the observers

	        public synchronized void removePanel(JPanel panel)
	        {
	             Enumeration e = panels.keys();

	             while (e.hasMoreElements()) {
	                  Object key = e.nextElement();

	                  if (panels.get(key) == panel) {
	                       panels.remove(key);
	                       setChanged();
	                       notifyObservers(new PanelRegistryEvent(
	                            PanelRegistryEvent.REMOVE_PANEL,
	                            (String)key, panel));
	                       return;
	                  }
	             }
	        }
	        
	        
	     // removes a frame from the table and notifies the observers

	        public synchronized void removePanel(String name)
	        {
	             JPanel panel = (JPanel) panels.get(name);
	             if (panel == null) return;

	             panels.remove(name);

	             setChanged();
	             notifyObservers(new PanelRegistryEvent(
	                  PanelRegistryEvent.REMOVE_PANEL,
	                  name, panel));
	        }
	        
	     // finds a frame by name, or returns null if not found

	        public JPanel findPanel(String name)
	        {
	             return (JPanel) panels.get(name);
	        }

	   // lets you see all the frames in the registry

	        public Enumeration getFrames()
	        {
	             return panels.elements();
	        }

}
